﻿var __XmlHttp = false;
try{__XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");}catch (e){try{__XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}catch (e){__XmlHttp = false;}}
if(!__XmlHttp && typeof XMLHttpRequest != 'undefined'){try {__XmlHttp = new XMLHttpRequest();}catch(e){__XmlHttp = false;}}
if(!__XmlHttp && window.createRequest){try{__XmlHttp = window.createRequest();}catch(e){__XmlHttp = false;}}
function __HidePreview() {
    document.getElementById(PreviewDivExternal).style["display"] = "none";
}

function __Insert(strInsertText) {
    var objTextArea = document.getElementById(txtContent);
    if(objTextArea) {
        if(document.selection && document.selection.createRange) {
	        objTextArea.focus();
	        var objSelectedTextRange = document.selection.createRange();
	        var strSelectedText = objSelectedTextRange.text;
	        objSelectedTextRange.text = strInsertText + strSelectedText;
        }
        else {
            objTextArea.focus(); 
            var selStart = objTextArea.selectionStart;
			var strFirst = objTextArea.value.substring(0, objTextArea.selectionStart);
			var strSecond = objTextArea.value.substring(objTextArea.selectionStart);
			objTextArea.value = strFirst + strInsertText + strSecond;
			objTextArea.selectionStart = selStart + strInsertText.length;
			objTextArea.selectionEnd = selStart + strInsertText.length;
        }
    }
    document.getElementById(SpecialTagsDiv).style["display"] = "none";
    return false;
}
function __Wrap(strStartTag, strEndTag) {
    while(strStartTag.indexOf("$") != -1) {
        // The symbol '$' is used to replace double quotes inside javascript links
        strStartTag = strStartTag.replace("$", "\"");
    }
    var objTextArea = document.getElementById(txtContent);
    if(objTextArea) {
        if(document.selection && document.selection.createRange) {
	        objTextArea.focus();
	        var objSelectedTextRange = document.selection.createRange();
	        var strSelectedText = objSelectedTextRange.text;
	        if(strSelectedText.substring(0, strStartTag.length) == strStartTag && strSelectedText.substring(strSelectedText.length - strEndTag.length, strSelectedText.length) == strEndTag) {
	            objSelectedTextRange.text = strSelectedText.substring(strStartTag.length, strSelectedText.length - strEndTag.length);
	        }
	        else {
	            objSelectedTextRange.text = strStartTag + strSelectedText + strEndTag;
	        }
	        objSelectedTextRange.select();
        }
        else {
            objTextArea.focus(); 
            var selStart = objTextArea.selectionStart;
			var strFirst = objTextArea.value.substring(0, objTextArea.selectionStart);
			var strSelected = objTextArea.value.substring(objTextArea.selectionStart, objTextArea.selectionEnd);
			var strSecond = objTextArea.value.substring(objTextArea.selectionEnd);
			if(strSelected.substring(0, strStartTag.length) == strStartTag && strSelected.substring(strSelected.length - strEndTag.length, strSelected.length) == strEndTag) {
			    // Remove tags
			    strSelected = strSelected.substring(strStartTag.length, strSelected.length - strEndTag.length);
			    objTextArea.value = strFirst + strSelected + strSecond;
			    objTextArea.selectionStart = selStart;
			    objTextArea.selectionEnd = selStart + strSelected.length;
			}
			else {
			    objTextArea.value = strFirst + strStartTag + strSelected + strEndTag + strSecond;
			    objTextArea.selectionStart = selStart;
			    objTextArea.selectionEnd = selStart + strStartTag.length + strSelected.length + strEndTag.length;
			}
        }
    }
    document.getElementById(SpecialTagsDiv).style["display"] = "none";
    return false;
}
function __BuildLink() {
    var strTitle = window.prompt(__StringLinkTitle);
    if(strTitle == null) return false;
    var strUrl = window.prompt(__StringLinkUrl, "http://");
    if(strUrl == null) return false;
    __Insert("[" + strUrl + "|" + strTitle + "]");
    return false;
}

function __BuildAnchor() {
    var strId = window.prompt(__StringAnchorId, "#");
    if(strId == null) return;
    if(strId.substring(0, 1) != "#") strId = "#" + strId;
    return __Insert("[anchor|" + strId + "]");
}
function __BuildAnchorLink() {
    var strTitle = window.prompt(__StringAnchorTitle);
    if(strTitle == null) return false;
    var strId = window.prompt(__StringAnchorId, "#");
    if(strId == null) return false;
    if(strId.substring(0, 1) != "#") strId = "#" + strId;
    __Insert("[" + strId + "|" + strTitle + "]");
    return false;
}

function __BuildImage(align) {
    var strTitle = window.prompt(__StringImageTitle);
    if(strTitle == null) return false;
    var strUrl = window.prompt(__StringImageUrl, "http://");
    if(strUrl == null) return false;
    var strBigUrl = window.prompt(__StringImageBigUrl, "http://");
    if(strBigUrl == "http://" || strBigUrl == "") strBigUrl = null;
    var strResult = "[image" + align + "|" + strTitle + "|" + strUrl;
    if(strBigUrl != null) strResult = strResult + "|" + strBigUrl;
    strResult = strResult + "]";
    __Insert(strResult);
    return false;
}

function __AbsolutePosition(obj) {
    var pos = null; 
    if(obj != null) {
        pos = new Object();
        pos.top = obj.offsetTop;
        pos.left = obj.offsetLeft;
        pos.width = obj.offsetWidth;
        pos.height= obj.offsetHeight;
  
        obj = obj.offsetParent;
        while(obj != null) {
            pos.top += obj.offsetTop;
            pos.left += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }
    return(pos);
}

function __ToggleItem(link, div) {
    var objLink = document.getElementById(link);
    var objDiv = document.getElementById(div);
    if(objDiv.style["display"] == "none") {
        __HideMenus();
        var pos = __AbsolutePosition(objLink);
        var top = pos.top + pos.height;
        var left = pos.left;
	    objDiv.style["position"] = "absolute";
	    objDiv.style["top"] = top + "px";
	    objDiv.style["left"] = left + "px";
        objDiv.style["display"] = "";
    }
    else {
        objDiv.style["display"] = "none";
    }
    return false;
}
function __ToggleSpecialTags() {
    __ToggleItem("SpecialTagsLink", "SpecialTagsDiv");
    return false;
}

function __ToggleAnchors() {
    __ToggleItem("AnchorLink", "AnchorsDiv");
    return false;
}

function __ToggleImages() {
    __ShowImageDialog();
    return false;
    __ToggleItem("ImageLink", "ImagesDiv");
    return false;
}

function __TogglePageList() {
    __ToggleItem("PageListLink", "PageListDiv");
    return false;
}

function __ToggleSnippetList() {
    __ToggleItem("SnippetListLink", "SnippetListDiv");
    return false;
}

function __ToggleFileList() {
    __ToggleItem("FileLink", "FileListDiv");
    return false;
}

var toggled = "";

function __LinkPage(pg) {
    if(toggled == "1") {
        document.getElementById("ImageLinkInput").value = pg;
    }
    else {
        var title = window.prompt(__StringLinkTitle, "");
        if(title == null) return false;
        if(title != "") __Insert("[" + pg + "|" + title + "]");
        else __Insert("[" + pg + "]");
    }
    __TogglePageList();
    return false;
}

function __LinkFile(file) {
    if(toggled == "1") {
        document.getElementById("ImageLinkInput").value = file;
    }
    else {
        if(document.getElementById("AttachmentChk").checked) {
            __Insert("[attachment:" + file + "]");
            __ToggleFileList();
            return false;
        }
        var title = window.prompt(__StringLinkTitle, "");
        if(title == null) return false;
        if(title != "") __Insert("[{UP}" + file + "|" + title + "]");
        else __Insert("[{UP}" + file + "]");
    }
    __ToggleFileList();
    return false;
}

function getPageScroll(){

    var yScroll;
    var xScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;
    }

    arrayPageScroll = new Array(xScroll,yScroll) 
    return arrayPageScroll;
}

function getPageSize(){
	
    var xScroll, yScroll;
	
    if (window.innerHeight && window.scrollMaxY) {	
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
	
    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }	
	
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else { 
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){	
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }

    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
    return arrayPageSize;
}

//var prevCaret;

function __ShowImageDialog() {
    /*if(document.selection && document.selection.createRange) {
        var r = document.getElementById("ctl00_CphMaster_txtContent").createTextRange();
        prevCaret = r.getBookmark();
    }
    else prevCaret = null;*/
    __DisplayJsBrowserContent();
    __CenterAndShow(document.getElementById("ImageDialogDiv"));
    __ShowBG();
    toggled = "1";
    document.getElementById("ImageTitleInput").focus();
    document.getElementById("AttachmentChk").disabled = true;
    return false;
}

function __HideImageDialog() {
    /*if(prevCaret) {
        var r = document.getElementById("ctl00_CphMaster_txtContent").createTextRange();
        r.moveToBookmark(prevCaret);
        //r.select();
    }*/
    var diag = document.getElementById("ImageDialogDiv");
    diag.style["display"] = "none";
    __HideBG();
    toggled = "";
    document.getElementById("AttachmentChk").disabled = false;
    return false;
}

function __ShowBG() {
    var bg = document.getElementById("BackgroundDiv");
    bg.style["position"] = "absolute";
    bg.style["left"] = "0px";
    bg.style["top"] = "0px";
    bg.style["width"] = "100%";
    bg.style["height"] = getPageSize()[1] + "px";
    bg.style["display"] = "";
}

function __HideBG() {
    var bg = document.getElementById("BackgroundDiv");
    bg.style["display"] = "none";
}

function __CenterAndShow(div) {
    var pScroll = getPageScroll();
    var pSize = getPageSize();
    div.style["left"] = ((pScroll[0] + (pSize[2] - 618) / 2)) + "px";
    div.style["top"] = ((pScroll[1] + (pSize[3] - 360) / 2)) + "px";
    div.style["display"] = "";
}

var current = "txtImageUrl";

function __ShowAndSelectInput(sel) {
    current = sel;
    document.getElementById("ImageBrowserDiv").style["display"] = "";
    document.getElementById("BrowserControlsDiv").style["display"] = "";
    document.getElementById("InputControlsDiv").style["display"] = "none";
    return false;
}

function __SelectImage(val) {
    document.getElementById(current).value = val;
    return false;
}

function __Hide(cancel) {
    document.getElementById("ImageBrowserDiv").style["display"] = "none";
    document.getElementById("BrowserControlsDiv").style["display"] = "none";
    document.getElementById("InputControlsDiv").style["display"] = "";
    if(cancel) document.getElementById(current).value = "";
    return false;
}

function __Done(cancel) {
    if(cancel) {
        document.getElementById("ImageTitleInput").value = "";
        document.getElementById("ImageUrlInput").value = "";
        document.getElementById("ImageLinkInput").value = "";
        __HideImageDialog();
        return false;
    }
    var title = document.getElementById("ImageTitleInput").value;
    var url = document.getElementById("ImageUrlInput").value;
    var link = document.getElementById("ImageLinkInput").value;
    var imageType = "image";
    if(document.getElementById("RightImageRadio").checked) imageType = "imageright";
    if(document.getElementById("LeftImageRadio").checked) imageType = "imageleft";
    if(document.getElementById("AutoImageRadio").checked) imageType = "imageauto";
    if(document.getElementById("InlineImageRadio").checked) imageType = "image";
    if(title.length == 0 || url.length == 0) {
        window.alert(__EnterTitleAndUrl);
        return false;
    }
    if(link.length > 0) {
        __Insert("[" + imageType + "|" + title + "|" + url + "|" + link + "]");
    }
    else __Insert("[" + imageType + "|" + title + "|" + url + "]");
    document.getElementById("ImageTitleInput").value = "";
    document.getElementById("ImageUrlInput").value = "";
    document.getElementById("ImageLinkInput").value = "";
    __HideImageDialog();
    return false;
}

function __TogglePageListForImage() {
    __ToggleItem("PickPageLink", "PageListDiv");
    return false;
}

function __ToggleFileListForImage() {
    __ToggleItem("PickFileLink", "FileListDiv");
    return false;
}
function __IncreaseSize() {
    var box = document.getElementById(txtContent);
    box.rows = box.rows + 8;
    return false;
}
function __DecreaseSize() {
    var box = document.getElementById(txtContent);
    box.rows = box.rows - 8;
    return false;
}
function __AutoSize() {
    var box = document.getElementById(txtContent);
    var str = document.getElementById(txtContent).value;
    var count = str.split("\n").length + str.length / (box.cols * 2);
    if(count > 0) {
        box.rows = count + 6;
    }
    return false;
}

function __ResizeTextbox() {
    document.getElementById(txtContent).style["width"] = document.getElementById("MainTitle").scrollWidth - 24 + "px";
}
__ResizeTextbox();
window.onresize = __ResizeTextbox;
document.getElementById(txtContent).onmouseup = __HideMenus;

function __HideMenus() {
    document.getElementById("SpecialTagsDiv").style["display"] = "none";
    document.getElementById("AnchorsDiv").style["display"] = "none";
    document.getElementById("ImagesDiv").style["display"] = "none";
    document.getElementById("PageListDiv").style["display"] = "none";
    document.getElementById("SnippetListDiv").style["display"] = "none";
    document.getElementById("FileListDiv").style["display"] = "none";
}
try
{
document.getElementById(txtContent).selectionStart = 0;
document.getElementById(txtContent).selectionEnd = 0;
document.getElementById(txtContent).focus();
}catch(ex) { }
try {
    if(document.getElementById(txtPageName).value.length == 0 && !document.getElementById(txtPageName).hidden) {
        document.getElementById(txtPageName).focus();
    }
    else if(document.getElementById(txtTitle).value.length == 0 && !document.getElementById(txtTitle).hidden) {
        document.getElementById(txtTitle).focus();
    }
} catch(ex) { }

var __ClosePreviewLink = '<a href="#" onclick="javascript:document.getElementById(\'AjaxPreviewDiv\').style[\'display\'] = \'none\';" style="float: right; padding: 4px; background-color: #FFFFFF;"><b>X</b></a>';
        
function __DetectScripts() {
    var data = document.getElementById(txtContent).value;
    data = data.toLowerCase();
    if(data.indexOf("<script") != -1) return true;
    else return false;
}

function __LoadPreview() {
    if(!__XmlHttp) {alert('aa');
        return true;
    }
    if(!__ScriptAllowed && __DetectScripts()) {
        document.getElementById("ResultSpan").className = "resulterror display";
        document.getElementById("ResultSpan").innerHTML = __ScriptDetected;
        return false;
    }
    __ResetAjaxPreview();
    document.getElementById("PreviewDivExternal").style["display"] = "";
    
    var loc = document.location.href;
    loc = loc.replace("#Prev", "");
	loc = loc.replace("#", "");
	document.location.href = loc + "#Prev";
    
    __XmlHttp.open("POST", "Preview.htm", true);
    __XmlHttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
    __XmlHttp.onreadystatechange = function() {
	    if(__XmlHttp.readyState == 4 || __XmlHttp.readyState == "complete") {
		    if(__XmlHttp.status == 200 || __XmlHttp.status == 0) {
			    document.getElementById("PreviewDiv").innerHTML = __XmlHttp.responseText;
			    document.getElementById("ResultSpan").className = "resultok display";
			    document.getElementById("ResultSpan").innerHTML = __PreviewCreated;
			    var loc = document.location.href;
			    loc = loc.replace("#Prev", "");
			    loc = loc.replace("#", "");
			    document.location.href = loc + "#Prev";
		    }
		    else {
		        window.alert("AJAX POST Returned: " + __XmlHttp.status + "\n" + __XmlHttp.statusText);
			    return true;
		    }
	    }
    }
    var content = document.getElementById(txtContent).value;
    while(content.indexOf("&") != -1) {
        content = content.replace("&", "%26");
    }
    while(content.indexOf("+") != -1) {
        content = content.replace("+", "%2B");
    }
    __XmlHttp.send("Text=" + content);
    return false;
}

function __ResetAjaxPreview() {
    document.getElementById("PreviewDiv").innerHTML = '<p style="text-align: center;"><img src="Images/Wait.gif" alt="Wait" /></p>';
}
var submitted = false;
function __UnloadPage(e) {
	if(!submitted) {
		e.returnValue = " ";
	}
}

function __SetSubmitted() {
	submitted = true;
}

document.getElementById(btnSave).onclick = __SetSubmitted;
if(document.getElementById(btnSaveAndContinue)) {
	document.getElementById(btnSaveAndContinue).onclick = __SetSubmitted;
}
document.getElementById(btnCancel).onclick = __SetSubmitted;
if(document.getElementById(btnNewCategory)) {
    // btnNewCategory might not be visible
    document.getElementById(btnNewCategory).onclick = __SetSubmitted;
}